package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"shop-city/models"
	"strconv"
	"time"
)

type ShopController struct {
	beego.Controller
}

func (c *ShopController) Get() {

}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *ShopController) Add() {

	name := c.GetString("name")
	price, _ := c.GetFloat("price")
	img := c.GetString("img")
	shop := models.Shop{
		Name:  name,
		Price: price,
		Img:   img,
	}
	err := models.DB.Create(&shop).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: shop,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}
func (c *ShopController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	name := c.GetString("name")
	where := map[string]string{}
	if name != "" {
		where["name"] = name
	}
	var count int64
	models.DB.Model(&models.Shop{}).Count(&count)
	offest := (page - 1) * size

	var allshop []models.Shop
	err := models.DB.Order("price desc").Offset(offest).Limit(size).Find(&allshop).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "查询失败",
		}
		c.ServeJSON()
	} else {
		pageData := make(map[string]interface{})
		pageData["count"] = count
		pageData["shop"] = allshop
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: allshop,
		}
		c.ServeJSON()
	}
}
func (c *ShopController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件过大 请重新上传",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	ext := map[string]bool{
		".png": true,
		".jpg": true,
	}
	suffix := filepath.Ext(header.Filename)
	if !ext[suffix] {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件类型错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgsrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件上传成功",
			Data: imgsrc,
		}
		c.ServeJSON()
	}
}
func (c *ShopController) Info() {
	id, _ := c.GetInt("id")
	key := "goods" + strconv.Itoa(id)
	var goods models.Shop
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		err = models.DB.Where("id=?", id).First(&goods).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			jsonByte, _ := json.Marshal(&goods)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Minute*10)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-Mysql",
				Data: goods,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &goods)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-Redis",
			Data: goods,
		}
	}
}
